from fastapi import APIRouter, UploadFile, File, Form
from models.ai.utils.common import R, logger, saveRequestFile
from pydantic import BaseModel
from typing import List
import os

router = APIRouter(prefix="/test", tags=["默认路由"])
class _CreateTask(BaseModel):
    name: str
    age: str

@router.post("/jsonList")
async def index(vo: List[_CreateTask]):
    logger.info(f"jsonList = {vo}")
    return R.success()

@router.post("/json")
async def index(vo: _CreateTask):
    logger.info(f"json = {vo}")
    return R.success()

# 定义保存文件的目录
UPLOAD_DIR = "uploads"
# 如果目录不存在，则创建该目录
if not os.path.exists(UPLOAD_DIR):
    os.makedirs(UPLOAD_DIR)

@router.post("/file")
async def index(file: UploadFile = File(...),
                other: str = Form(None)):
    filePath = await saveRequestFile(file, distPath=UPLOAD_DIR)
    logger.info(f"filePath = {filePath}, other = {other}")
    return R.data("测试")
